package com.xxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxx.entity.MPage;
import com.xxx.entity.Remark;
import com.xxx.dao.RemarkDao;
import com.xxx.entity.dto.RemarkDto;
import com.xxx.entity.dto.RemarkDto;
import com.xxx.entity.query.RemarkQuery;
import com.xxx.service.IRemarkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@Service
@Transactional
public class RemarkServiceImpl extends ServiceImpl<RemarkDao, Remark> implements IRemarkService {


    @Autowired
    private RemarkDao remarkDao;

    @Override
    public Boolean delete(Integer remarkId) {
//        LambdaQueryWrapper<Remark> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Remark::getRemarkFather,remarkId);
//        List<Remark> list = remarkDao.selectList(lqw);
//
//        for(Remark remark : list){
//            delete(remark.getRemarkId());
//        }

        return  remarkDao.deleteById(remarkId) > 0;
    }

    @Override
    public MPage selectByPage(Integer current, Integer size) {
        int total = remarkDao.selectCount(null);
        List<RemarkDto> list = remarkDao.selectByPage((current-1)*size,size);
        MPage page = MPage.builder()
                .current(current)
                .size(size)
                .total(total)
                .pages(total/size+total%size>0?1:0)
                .records(list)
                .build();
        return page;
    }

    @Override
    public MPage selectByCondition(RemarkQuery remarkQuery) {
        int current = remarkQuery.getCurrent();
        int size = remarkQuery.getSize();
        remarkQuery.setCurrent(null);
        List<RemarkDto> list = remarkDao.selectByCondition(remarkQuery);
        int total = list.size();
        remarkQuery.setCurrent((current-1)*size);
        list = remarkDao.selectByCondition(remarkQuery);
        MPage page = MPage.builder()
                .current(current)
                .size(size)
                .total(total)
                .pages(total/size+total%size>0?1:0)
                .records(list)
                .build();
        return page;
    }
}
